<?php
namespace app\admin\controller;
use think\Db;
use think\Validate;
use app\admin\controller\Base;
use app\admin\model\Pictureinfo as PictureinfoModel;

class Pictureinfo extends Base{

	public function lists(){
		$list=PictureinfoModel::paginate(10);
		$this->assign('list',$list);
		return $this->fetch('listpic');
	}



	public function edit(){
		$data=input();
		$res=db('pictureinfo')->find($data['id']);
		
		if(request()->isPost()){
			$data=[
				'id'=>input('id'),
				'sportitem'=>input('sportitem'),
				'phototime'=>input('phototime'),
				'photoaddress'=>input('photoaddress'),
			];
			if($_FILES['uploadImage']['tmp_name']!=''&&isset($_FILES['uploadImage']['tmp_name'])){
				$file=request()->file('uploadImage');
				$info=$file->move(ROOT_PATH.'public'.DS.'static/uploads');
				$data['photourl']='/static/uploads/'.$info->getSaveName();
			}

			$result=db('pictureinfo')->update($data);
			if ($result) {
				if ($res['photourl']!='' && file_exists(ROOT_PATH.'public'.$res['photourl'])) {
					unlink(ROOT_PATH.'public'.$res['photourl']);
				}
				return $this->success('修改成功！','lists',array('id'=>$data['id']),1,array(),$data['id']);
			}else{
				return $this->success('修改失败！');
			}
		}
		$this->assign('type',$data['type']);
		$this->assign('pictureinfo',$res);
		return $this->fetch('edits');
	}



	public function upload(){
	    // 获取表单上传文件 例如上传了001.jpg
	    $file = request()->file('uploadImage');
	    // 移动到框架应用根目录/public/uploads/ 目录下
	    if($file){
	        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
	        if($info){
	            $result=array('msg'=>'上传成功!','data'=>ROOT_PATH . 'public' . DS . 'uploads'.$info->getSaveName(),'success'=>'0');

	            // 输出 jpg
            // echo $info->getExtension();
            // // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
            // echo $info->getSaveName();
	            // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
	            // echo $info->getSaveName();
	        }else{
	        	$result=array('msg'=>'上传失败!','data'=>$file->getError(),'success'=>'1');
	            // 上传失败获取错误信息
	            // echo $file->getError();
	        }
	    }
	    return $result;
	}
}